﻿@inject DialogService DialogService
@inject ITechnologyService TechnologyService
@inject IStandardBomService StandardBomService

<RadzenStack>
    <RadzenStack>
        <RadzenRow>
            <RadzenColumn>
                <RadzenButton Variant="Variant.Outlined" Click="SubmitHandle">保存</RadzenButton>
                <RadzenButton Variant="Variant.Outlined" Click="CancelHandle">取消</RadzenButton>
            </RadzenColumn>
        </RadzenRow>
    </RadzenStack>
    <RadzenStack>
        <RadzenFieldset AllowCollapse="true" Style="width: 100%; margin: 40px auto;" ExpandTitle="Expand orders." CollapseTitle="Collapse orders."
                        ExpandAriaLabel="Expand the list of orders." CollapseAriaLabel="Collapse the list of orders.">
            <HeaderTemplate>
                <span class="d-inline-flex align-items-center align-middle">
                    <RadzenIcon Icon="account_box" class="me-1" /><b>基本信息</b>
                </span>
            </HeaderTemplate>
            <ChildContent>
                <RadzenStack class="rz-mb-2">
                    <RadzenRow>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="工艺流程编码" Style="width: 100%;">
                                <RadzenTextBox @bind-Value="_editRequestDto.Code"></RadzenTextBox>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="工艺流程名称" Style="width: 100%;">
                                <RadzenTextBox @bind-Value="_editRequestDto.Name"></RadzenTextBox>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="流程类型" Style="width: 100%;">
                                <RadzenNumeric @bind-Value="_editRequestDto.ProcessType"></RadzenNumeric>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="生产类型" Style="width: 100%;">
                                <RadzenNumeric @bind-Value="_editRequestDto.ProductionType"></RadzenNumeric>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="生产产量" Style="width: 100%;">
                                <RadzenNumeric @bind-Value="_editRequestDto.Quality"></RadzenNumeric>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12">
                            <RadzenFormField Text="描述" Style="width: 100%;">
                                <RadzenTextArea @bind-Value="_editRequestDto.Description"></RadzenTextArea>
                            </RadzenFormField>
                        </RadzenColumn>
                    </RadzenRow>
                </RadzenStack>
            </ChildContent>
        </RadzenFieldset>
    </RadzenStack>
    <RadzenStack>
        <RadzenFieldset AllowCollapse="true" Style="width: 100%; margin: 40px auto;" ExpandTitle="Expand orders." CollapseTitle="Collapse orders."
                        ExpandAriaLabel="Expand the list of orders." CollapseAriaLabel="Collapse the list of orders.">
            <HeaderTemplate>
                <span class="d-inline-flex align-items-center align-middle">
                    <RadzenIcon Icon="account_box" class="me-1" /><b>BOM信息</b>
                </span>
            </HeaderTemplate>
            <ChildContent>
                <RadzenStack>
                    <RadzenRow>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="BOM编码" Style="width: 100%;">
                                <RadzenDropDownDataGrid @bind-Value=_editRequestDto.StandardBom.Id LoadData=@LoadData AllowFiltering="true"
                                                        Data="_standardBoms" Count="_count"
                                                        TextProperty="Code" ValueProperty="Id">
                                    <Columns>
                                        <RadzenDropDownDataGridColumn Property="Code" Title="Code" Width="100px" />
                                        <RadzenDropDownDataGridColumn Property="Name" Title="Name" Width="100px" />
                                    </Columns>
                                </RadzenDropDownDataGrid>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="BOM名称" Style="width: 100%;">
                                <RadzenTextBox @bind-Value="_editRequestDto.StandardBom!.Name"></RadzenTextBox>
                            </RadzenFormField>
                        </RadzenColumn>
                    </RadzenRow>
                </RadzenStack>
            </ChildContent>
        </RadzenFieldset>
    </RadzenStack>
    <RadzenStack>
        <RadzenFieldset AllowCollapse="true" Style="width: 100%; margin: 40px auto;" ExpandTitle="Expand orders." CollapseTitle="Collapse orders."
                        ExpandAriaLabel="Expand the list of orders." CollapseAriaLabel="Collapse the list of orders.">
            <HeaderTemplate>
                <span class="d-inline-flex align-items-center align-middle">
                    <RadzenIcon Icon="account_box" class="me-1" /><b>明细信息</b>
                </span>
            </HeaderTemplate>
            <ChildContent>
                <RadzenStack>
                    <RadzenRow>
                        <RadzenColumn>
                            <RadzenButton Variant="Variant.Outlined" Click="AddHandle">Add</RadzenButton>
                        </RadzenColumn>
                    </RadzenRow>
                    <RadzenDataGrid AllowFiltering="true" AllowColumnResize="true" AllowAlternatingRows="false" FilterMode="FilterMode.Advanced" AllowSorting="true" PageSize="5"
                                    AllowPaging="true" PagerHorizontalAlign="HorizontalAlign.Left" ShowPagingSummary="true"
                                    Data="@_editRequestDto.Items" TItem="TechnologyDetailEditRequestDto" ColumnWidth="300px" LogicalFilterOperator="LogicalFilterOperator.Or">
                        <Columns>
                            <RadzenDataGridColumn TItem="TechnologyDetailEditRequestDto" Property="ProcessCode" Title="工序编号" Frozen="true" Width="160px" />
                            <RadzenDataGridColumn TItem="TechnologyDetailEditRequestDto" Property="ProcessName" Title="工序名称" Width="160px" />
                            <RadzenDataGridColumn TItem="TechnologyDetailEditRequestDto" Property="Sort" Title="排序" Width="200px" />
                            <RadzenDataGridColumn TItem="TechnologyDetailEditRequestDto" Property="TakeTime" Title="耗时" Width="120px" />
                            <RadzenDataGridColumn TItem="TechnologyDetailEditRequestDto" Filterable="false" Sortable="false" Width="100px" TextAlign="TextAlign.Center">
                                <Template Context="context">
                                    <RadzenButton ButtonStyle="ButtonStyle.Primary" Icon="edit" Size="ButtonSize.Medium"
                                                  Shade="Shade.Lighter" Variant="Variant.Flat"
                                                  Click=@(args => EditHandle(args, context)) @onclick:stopPropagation="true" />
                                    <RadzenButton ButtonStyle="ButtonStyle.Danger" Icon="delete" Size="ButtonSize.Medium"
                                                  Shade="Shade.Lighter" Variant="Variant.Flat"
                                                  Click=@(args => DeleteHandler(args, context)) @onclick:stopPropagation="true" />
                                </Template>
                            </RadzenDataGridColumn>
                        </Columns>
                    </RadzenDataGrid>
                </RadzenStack>
            </ChildContent>
        </RadzenFieldset>
    </RadzenStack>
</RadzenStack>

@code {
    private int _initKey = -1;
    private TechnologyEditRequestDto _editRequestDto = new();
    private IList<StandardBomInfoResponseDto> _standardBoms = new List<StandardBomInfoResponseDto>();
    private int _count = 0;

    [CascadingParameter]
    public TechnologyIndex? TechnologyIndex { get; set; }

    async Task AddHandle(MouseEventArgs args)
    {
        TechnologyDetailEditRequestDto itemData = new()
            {
                Key = _initKey
            };
        var result = await DialogService.OpenAsync<TechnologyDetailEdit>("添加工序");
        if (result != null)
        {
            _initKey--;
            itemData = (TechnologyDetailEditRequestDto)result;
            _editRequestDto.Items.Add(itemData);
        }
    }

    async Task DeleteHandler(MouseEventArgs args, TechnologyDetailEditRequestDto itemData)
    {
    }

    async Task EditHandle(MouseEventArgs args, TechnologyDetailEditRequestDto itemData)
    {
        var result = await DialogService.OpenAsync<TechnologyDetailEdit>("修改工序",
            new Dictionary<string, object>()
                        {
                    { "Model",itemData }
                        });
        if (result != null)
        {
            itemData = (TechnologyDetailEditRequestDto)result;
        }
    }

    async Task LoadData(LoadDataArgs args)
    {
        var result = await StandardBomService.QueryStandardBomsByPage(new StandardBomSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            _standardBoms = result.Data!;
            _count = result.TotalCount;
        }

        await InvokeAsync(StateHasChanged);
    }

    // private async Task<IEnumerable<StandardBomInfoResponseDto>> StandardBomSearch(string value)
    // {
    //     var result = await StandardBomService.QueryStandardBoms(new StandardBomSearchRequestDto());
    //     if (result != null && result.Succeeded)
    //     {
    //         return result.Data!;
    //     }
    //     return new List<StandardBomInfoResponseDto>();
    // }

    void CancelHandle()
    {
        TechnologyIndex?.SetDynamicComponentType(typeof(TechnologyTable));
    }

    async Task SubmitHandle()
    {
        var key = TechnologyIndex?.GetKey();

        if (key > 0)
        {
            var result = await TechnologyService.UpdateTechnology(key.Value, _editRequestDto);
            if (result != null && result.Succeeded)
            {
            }
        }
        else
        {
            var result = await TechnologyService.CreateTechnology(_editRequestDto);
            if (result != null && result.Succeeded)
            {
            }
        }
    }

    protected override async Task OnParametersSetAsync()
    {
        _editRequestDto = new TechnologyEditRequestDto();
        var key = TechnologyIndex?.GetKey();
        if (key > 0)
        {
            var result = await TechnologyService.GetTechnology(key.Value);
            if (result != null && result.Succeeded && result.Data != null)
            {
                _editRequestDto.Code = result.Data.Code;
                _editRequestDto.Name = result.Data.Name;
                _editRequestDto.StandardBom.Id = result.Data.StandardBomId;
                _editRequestDto.StandardBom.Code = result.Data.StandardBomCode;
                _editRequestDto.StandardBom.Name = result.Data.StandardBomName;
                _editRequestDto.Description = result.Data.Description;
                if (result.Data.Items != null)
                {
                    _editRequestDto.Items = result.Data.Items.Select(i => new TechnologyDetailEditRequestDto()
                        {
                            Key = i.Id,
                            ProcessId = i.Id,
                            Process = new ProcessInfoResponseDto()
                            {
                                Id = i.Id,
                                Code = i.ProcessCode,
                                Name = i.ProcessName
                            },
                            Sort = i.Sort,
                            TakeTime = i.TakeTime
                        }).ToList();
                }
            }
        }

        await base.OnParametersSetAsync();
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var result = await StandardBomService.QueryStandardBomsByPage(new StandardBomSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            _standardBoms = result.Data!;
            _count = result.TotalCount;
        }
    }

}